package com.letu.core.encrypt;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;

public class RSAEncrypt {

    /**
     * 用于封装随机产生的公钥与私钥
     */
//    private static Map<Integer, String> keyMap = new HashMap<Integer, String>();

    public static void main(String[] args) throws Exception {
        //生成公钥和私钥
        Map<Integer, String> keyMap = AesEncryptUtil.genKeyPair();
        //加密字符串
        String message = "http://李刚1231#@123.com";

        message = java.net.URLDecoder.decode(message ,"UTF-8");

        System.out.println("随机生成的公钥为:" + keyMap.get(0));
        System.out.println("随机生成的私钥为:" + keyMap.get(1));
        String messageEn = encrypt(message, keyMap.get(0));
        System.out.println("加密前的字符串为:" + message);
        System.out.println("加密后的字符串为:" + messageEn);
        String messageDe = decrypt(messageEn, keyMap.get(1));
        System.out.println("还原后的字符串为:" + messageDe);

        String msg = "LbXdxUZSdPwKhFcgSmQPXwmMwPWxDsr0+moPAAs2HysZJz98B3NKW1rqUIUXby3fUl9qBIIkJG7EBDWw2s1UlnDM1eTY/EFyKpqs5ixTTvjq05xlcCYe2YTw7HMMVvH1zQtMjzrlKWo3I+AfZnq1IbWa5/CjsEtVLeJD8rncl3Q=";
        String privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBANVoV1DVY82mAJxDnPA31/huNIkYdxOM8PQ1hYRT7AYYVNGEWimEzXF1pPH7HCUiQ8c0OssVw46zk+QrOxojdegA1e/UOmU01aIq1RIldJk9wlvYT0VmJOPJ5AQSjlj/1xtpQMpWl267ApiBiCF9twynqINRCOg3oBB5cbh/xKzlAgMBAAECgYEAixlYmHKOe/OwwCn9+EGd/uruSVHuA4tBXxwnq+n7lLl1R59qGVfbhRG2cpdHcBjWbthnXgoQjkoUootfGZl1dpOkn1KvPYW74jsy+KiqdzKgM1vOS7P9jUab5DXniA777IpEdAOwDu10Ii0lCO6wmtExue+V1roI3FAk8U+tnL0CQQDsP7W1ZJ6zo6XHuNvIla7fVMUPKLxyrFOwGqiR5erzHIHKJhm4qvFoW0YlmaYq4C79dl8wOzDr2xKDFRN0AD73AkEA5z/F8haly6ABGmQJ2XaJlqa/6kmaQD0luzL6WK4+RV/KxLQ1iIK3dQvx+qi3srFpu5iYcWv8x106dgD31y+QAwJAG9RFa2C28SJN3QEBotc/Cj/KFscNq9Rw50W8pbMXwhuRjmZ+kSpR1aEq2jMHR/Wwk1rmS6AtWxsrSxWofPmqcQJAFi1CgXX0jnOYx3wiia1Zgo4ZR5GpblGu5aX85jZHmIWYP4b8v0ux5lKv8RbvDv7+odP/gpkVk9T3/0sWeZeBGwJARkGfRPhVwUR+OD+GLlbEciw3Zjrn6KPqq/7uB2rjPuoQ9uFg5CIwwSbtnIxsKviQ5QQgjJ9QB/34nT/eH/q28Q==";
        System.out.println("还原后的字符串22为:" + decrypt(msg, privateKey));
    }

    /**
     * RSA公钥加密
     *
     * @param str       加密字符串
     * @param publicKey 公钥
     * @return 密文
     * @throws Exception 加密过程中的异常信息
     */
    public static String encrypt(String str, String publicKey) throws Exception {
        //base64编码的公钥
        byte[] decoded = Base64.decodeBase64(publicKey.getBytes());
        RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
        //RSA加密
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, pubKey);
        String outStr = Base64.encodeBase64String(cipher.doFinal(str.getBytes("utf-8")));
        return outStr;
    }

    /**
     * RSA私钥解密
     *
     * @param str        加密字符串
     * @param privateKey 私钥
     * @return
     * @throws Exception 解密过程中的异常信息
     */
    public static String decrypt(String str, String privateKey) throws Exception {
        //64位解码加密后的字符串
        byte[] inputByte = Base64.decodeBase64(str.getBytes("utf-8"));
        //base64编码的私钥
        byte[] decoded = Base64.decodeBase64(privateKey.getBytes());
        RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
        //RSA解密
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, priKey);
        String outStr = new String(cipher.doFinal(inputByte));
        return outStr;
    }
}